import { message } from 'antd';
import getPlacements from 'antd/lib/tooltip/placements';
export default {
  namespace: 'os',

  state: {
    workingOrder: [], // 当前作业的工单
    totalOrders: [], // 所有的工单
    showTotalOrdersFlag: false, // 是否显示所有的工单
  },
  effects: {
    *deleteOrder({ payload }, { call, put }) {
      const twoOrderList = yield call(window.eel.run_py, 'db', 'web_request_delete_order', [payload]);
      if (twoOrderList[1].length > 0) {
        message.success('删除成功', 3);
        yield put({
          type: 'updateOrderState',
          payload: twoOrderList,
        });
      } else {
        message.error('你要删除的工单不存在，请确认工单是否输入正确！', 3);
      }
    },

    *searchOrder({ payload }, { call, put }) {
      const twoOrderList = yield call(window.eel.run_py, 'db', 'web_request_search_order', [payload]);
      if (twoOrderList[1].length > 0) {
        message.success('搜索成功', 3);
        yield put({
          type: 'updateOrderState',
          payload: twoOrderList,
        });
      } else {
        message.error('你要搜索的工单不存在，请确认工单是否输入正确！', 3);
      }
    },

    *handleOrderStateMessage({ payload }, { call, put }) {
      const { msg } = payload;
      console.log('in handleOrderStateMessage msg = ', msg.val);
      const twoOrderList = yield call(window.eel.run_py, 'db', 'web_request_get_order_by_key', [msg.val]);
      yield put({
        type: 'updateOrderState',
        payload: twoOrderList,
      });
    },

    *saveOrder({ payload }, { call, put }) {
      const twoOrderList = yield call(window.eel.run_py, 'ph', 'web_request_save_order', [payload]);
      if(twoOrderList[0] === "ok")
      {
        yield put({
          type: 'updateOrderState',
          payload: twoOrderList[1],
        });
      }
      else {
        message.error(twoOrderList[1],10);
      }
      
    },

    *clickHistory({ payload }, { call, put }) {
      yield put({
        type: 'updateHistoryOrder',
      });
    },
  },
  reducers: {
    updateHistoryOrder(state, action) {
      return {
        ...state,
        showTotalOrdersFlag: true,
      };
    },
    updateOrderState(state, action) {
      return {
        ...state,
        totalOrders: action.payload[0],
        workingOrder: action.payload[1],
        showTotalOrdersFlag: false,
      };
    },
    updateChooseOrder(state, action) {
      return {
        ...state,
        workingOrder: action.payload,
      };
    },
    clickAddOrder(state, action) {
      return {
        ...state,
        workingOrder: action.payload,
        showTotalOrdersFlag: false,
      };
    },
  },
};
